import AxiosApi from '@/api/AxiosApi'
import type { IResult, ITableList } from '@/interfaces/IApi'
import type { IUserTableItem0 } from '@/interfaces/IUser'

export async function getAuthCode(): Promise<string | null> {
  const result = await AxiosApi.get('/users/authcode').catch(() => {})
  if (result) return result.data
  return null
}

export async function getAuthCodeResult(code: string) {
  const result = await AxiosApi.post('/users/authCodeResult', { code })
  return result.data
}

export async function login(user: string, password: string) {
  const result = await AxiosApi.post('/user/login', { user, password })
  return result.data
}
export async function getUserList(page: number, size: number): Promise<ITableList> {
  const result = await AxiosApi.get('/user/list', { params: { page, size } })
  return result.data.data
}
export async function getUserList1() {
  const result = await AxiosApi.get('/user/rlist1')
  return result.data.data
}

export async function rUserList() {
  const result = await AxiosApi.get('/user/rlist')
  return result.data.data
}

export async function searchUser(username:string) {
  const result = await AxiosApi.get('/user/' + username)
  console.log(result)
  return result.data.data
}

export async function deleteUsers(ids: number[]): Promise<IResult> {
  const result = await AxiosApi.post('/user/delbatch', ids)
  return result.data
}

export async function deleteUser(id: number): Promise<IResult> {
  const result = await AxiosApi.delete('user/' + id)
  return result.data
}

export async function addUser(formData: IUserTableItem0): Promise<IResult> {
  const result = await AxiosApi.post('/user/create', formData)
  return result.data
}

export async function editUser(formData:any): Promise<IResult> {
  const result = await AxiosApi.put('/user/' + formData.id, formData)
  return result.data
}
